home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / mail / mh / contrib / multimedia / mhweather.shar / mail.weather.ext next >
Text File  |  1993-03-15  |  7KB  |  329 lines

  1. #!/usr/bin/perl
  2. #
  3. # mail.weather.ext - mail today's weather report using external body parts
  4. #
  5. # Usage: mail.weather [-debug] [-help] [-key file] [-verbose] address ...
  6. #
  7. # Author: Jerry Sweet <jsweet@irvine.com>
  8. #
  9. # $Header: /sparc/users/jsweet/src/weather/mhweather/RCS/mail.weather.ext,v 1.16 1993/03/07 08:43:40 jsweet Exp $
  10.  
  11. #
  12. # Subprograms
  13. #
  14.  
  15. sub gripe {
  16.   print STDERR "$prog: ", @_, "\n";
  17. }
  18.  
  19. sub barf {
  20.   &gripe(@_);
  21.   exit 1;
  22. }
  23.  
  24. sub cmd {
  25.   local($_, $exit_on_error) = @_;
  26.   local($st);
  27.  
  28.   if ($debug || $verbose) {
  29.     print "\t$_\n";
  30.   }
  31.  
  32.   $st = system "$_";
  33.  
  34.   if ($st >> 8 && $exit_on_error) {
  35.     split;
  36.     &barf("$_[0] failed with status ", $st >> 8);
  37.   }
  38. }
  39.  
  40. sub sh {
  41.   # Shell command, exit on error.
  42.  
  43.   local($_) = @_;
  44.   &cmd($_, 1);
  45. }
  46.  
  47. sub one_hour_earlier {
  48.   local($tt_hour, $tt_mday, $tt_mon) = @_;
  49.  
  50.   if (--$tt_hour < 0) {
  51.     $tt_hour = 23;
  52.     if (--$tt_mday < 0) {
  53.       if (--$tt_mon < 0) {
  54.         $tt_mon = 11;
  55.       }
  56.       $tt_mday = $lastday[$tt_mon];
  57.     }
  58.   }
  59.  
  60.   ($tt_hour, $tt_mday, $tt_mon);
  61. }
  62.  
  63. sub read_profile {
  64.   # Defines associative array %PROFILE_COMPONENT indexed by lower case
  65.   # component name.  Continuation lines are tacked on.  New
  66.   # occurrences of a component wipe out previous occurrences of the
  67.   # same component, but probably shouldn't.
  68.  
  69.   local($profile) = defined $ENV{'MH'} ? $ENV{'MH'} : 
  70.                      $ENV{'HOME'} . "/.mh_profile";
  71.   local($profile_component) = 'x-bogus';
  72.   local($_);
  73.   
  74.   &barf("can't open profile \"$profile\" - $!")
  75.     unless open(PROFILE, "<$profile");
  76.  
  77.   while (<PROFILE>) {
  78.     /^(\S+):\s*/ && do {
  79.             ($profile_component = $1) =~ tr/A-Z/a-z/;
  80.       $_ = $';
  81.       s/\s+$//;
  82.             $PROFILE_COMPONENT{$profile_component} = $_;
  83.         };
  84.   
  85.     /^[ \t]/ && do {
  86.       s/\s+$//;
  87.             $PROFILE_COMPONENT{$profile_component} .= $_;
  88.         };
  89.   }
  90. }
  91.  
  92. sub ext_local_file {
  93.   local($type, $explanation, $name, $site) = @_;
  94.   local($continue);
  95.  
  96.   $continue = $site ? "; \\\\" : '';
  97.  
  98.   print FORM <<"xxEndExternLocalFile";
  99. #@$type [$explanation] \\
  100.     access-type=local-file; \\
  101.     name="$name"$continue
  102. xxEndExternLocalFile
  103.  
  104.   if ($site) {
  105.     print FORM "\tsite=\"$site\"\n";
  106.   }
  107. }
  108.  
  109. sub ext_anon_ftp {
  110.   local($type, $explanation, $site, $dir, $name, $expiration, $size) = @_;
  111.   local($continue);
  112.  
  113.   $continue = ($expiration || $size) ? "; \\" : '';
  114.  
  115.   print FORM <<"xxEndExternAnonFTPxx";
  116. #@${type} [${explanation}] \\
  117.     access-type=anon-ftp; \\
  118.     name="${name}"; \\
  119.     directory="${dir}"; \\
  120.     site="${site}"${continue}
  121. xxEndExternAnonFTPxx
  122.  
  123.   $continue = $size ? "; \\" : '';
  124.  
  125.   if ($expiration) {
  126.     print FORM "\texpiration=\"${expiration}\"${continue}\n";
  127.   }
  128.  
  129.   if ($size) {
  130.     print FORM "\tsize=${size}\n";
  131.   }
  132. }
  133.  
  134. sub ftp_gif {
  135.   local($explanation, $name, $exp, $size) = @_;
  136.  
  137.   &ext_anon_ftp('image/gif', $explanation, $wsite, $wdir, $name, $exp, $size);
  138. }
  139.  
  140. #
  141. # Main
  142. #
  143.  
  144. ($prog = $0) =~ s|.*/||;
  145.  
  146. $paths = '/usr/local/bin/mh:/usr/local/bin';
  147.  
  148. $ENV{'MHCONTEXT'} = '/dev/null';    # No context changes here...
  149. $ENV{'PATH'} = $ENV{'HOME'} . '/bin:' . $paths . ':' . $ENV{'PATH'};
  150.  
  151. $wsite = 'vmd.cso.uiuc.edu';
  152. $wdir  = 'wx';
  153. $wxkey = 'WXKEY.GIF';
  154.  
  155. # Check the profile.
  156.  
  157. &read_profile;
  158. if (defined $PROFILE_COMPONENT{$prog}) {
  159.   push(@Args, split(' ', $PROFILE_COMPONENT{$prog}));
  160. }
  161.  
  162. # Check the command line arguments.
  163.  
  164. push(@Args, @ARGV);
  165.  
  166. while ($_ = shift @Args) {
  167.   /^-debug$/    && do { $debug++; next; };
  168.  
  169.   /^-help$/    && do {
  170.       print <<"xxEndHelpxx";
  171. $prog - mail today's weather report
  172. Usage: $prog [-(no)debug] [-help] [-key file] [-(no)verbose] address ...
  173. Options:
  174.   -debug        print the message draft and invoke default whatnow
  175.   -help         print this message
  176.   -key file     local path to weather key image file; FTP-ed if omitted
  177.   -verbose    print shell commands as they're executed
  178.   address       destination e-mail address(es)
  179. xxEndHelpxx
  180.  
  181.       exit 1;
  182.     };
  183.  
  184.   /^-key$/ && do {
  185.     $keyfile = shift @Args;
  186.     next;
  187.     };
  188.  
  189.   /^-nodebug$/ && do { --$debug; next; };
  190.   /^-noverbose$/ && do { --$verbose; next; };
  191.  
  192.   /-verbose$/ && do { ++$verbose; next; };
  193.  
  194.   /^[^\-]/    && do {
  195.     push(@address, $_);
  196.     next;
  197.     };
  198.  
  199.   &barf("unrecognized command line option \"$_\"");
  200. }
  201.  
  202. $debug = $debug > 0;
  203. $verbose = $verbose > 0;
  204.  
  205. &barf("no destination e-mail address specified") 
  206.     unless $#address >= 0;
  207.  
  208. if ($keyfile && ! -r $keyfile) {
  209.   $keyfile = '';
  210. }
  211.  
  212. $recipients = join (', ', @address);
  213.  
  214. $formfile = "/tmp/$prog.$$";
  215.  
  216. &barf("can't open temporary formfile $formfile - $!") unless
  217.     open(FORM, ">$formfile");
  218.  
  219. # Figure out which file to get; if we're not past the half-hour mark,
  220. # use the previous hour's file.
  221.  
  222. ($tt_sec,$tt_min,$tt_hour,$tt_mday,$tt_mon,$tt_year,$tt_wday,
  223.     $tt_yday,$tt_isdst) = gmtime(time);
  224.  
  225. @lastday = (31, ($tt_year % 4 == 0) ? 29 : 28, 
  226.         31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  227.  
  228. if ($tt_min < 30) {
  229.   ($tt_hour, $tt_mday, $tt_mon) = 
  230.     &one_hour_earlier($tt_hour, $tt_mday, $tt_mon);
  231. }
  232.  
  233. $datestr = sprintf("%02d%02d%02d", $tt_mon + 1, $tt_mday, $tt_hour);
  234. $gmt = sprintf("%02d/%02d %02d00 GMT", $tt_mon + 1, $tt_mday, $tt_hour);
  235. $sagmt = sprintf("%02d00 GMT %02d/%02d", $tt_hour, $tt_mon + 1, $tt_mday);
  236.  
  237. $surface_analysis_gif = sprintf("SA%s.GIF", $datestr);
  238.  
  239. ($ett_hour, $ett_mday, $ett_mon) = 
  240.     &one_hour_earlier($tt_hour, $tt_mday, $tt_mon);
  241.  
  242. $e_datestr = sprintf("%02d%02d%02d", $ett_mon + 1, $ett_mday, $ett_hour);
  243. $egmt = sprintf("%02d00 GMT %02d/%02d", $ett_hour, $ett_mon + 1, $ett_mday);
  244.  
  245.  
  246. $satellite_photo_gif = sprintf("CI%s.GIF", $e_datestr);
  247.  
  248. @m_name = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
  249.         'Sep', 'Oct', 'Nov', 'Dec');
  250.  
  251. $exp_hour = $tt_hour;
  252. $exp_mday = $tt_mday;
  253. $exp_mon  = $tt_mon;
  254. $exp_year = $tt_year;
  255.  
  256. if (++$exp_mday > $lastday[$exp_mon]) {
  257.   $exp_mday = 1;
  258.   if (++$exp_mon > 11) {
  259.     $exp_mon = 1;
  260.     if (++$exp_year > 99) {
  261.       $exp_year = 0;
  262.     }
  263.   }
  264. }
  265.  
  266. $expire_time = sprintf(
  267.     "%d %s %02d %02d:%02d:%02d GMT",
  268.     $exp_mday, $m_name[$exp_mon], $exp_year,
  269.     $exp_hour, 0, 0);
  270.  
  271. $inum = $keyfile ? 'two' : 'three';
  272.  
  273.  
  274. # Print the mhn composition template:
  275.  
  276. print FORM <<"xxEndForm10xx";
  277. To: $recipients
  278. Subject: weather report: $gmt
  279. --------
  280. #<text/richtext [explanation]
  281. <paragraph>You may FTP $inum images:</paragraph>
  282. <paragraph><indent><fixed>
  283. $surface_analysis_gif: surface analysis at $sagmt<nl>
  284. xxEndForm10xx
  285.  
  286. if (! $keyfile) {
  287.   print FORM <<"xxEndForm15xx";
  288. $wxkey:    map symbol key<nl>
  289. xxEndForm15xx
  290. }
  291.  
  292. print FORM <<"xxEndForm20xx";
  293. $satellite_photo_gif: satellite photo at $egmt
  294. </fixed></indent></paragraph>
  295. #
  296. #begin [weather report: $gmt] parallel
  297. xxEndForm20xx
  298.  
  299. &ftp_gif('surface analysis + radar summary', 
  300.     $surface_analysis_gif, $expire_time, 50000);
  301.  
  302. $keyfile ?
  303.   &ext_local_file('image/gif', 'weather map symbol key', $keyfile) :
  304.   &ftp_gif('weather map symbol key', $wxkey, 0, 18867);
  305.  
  306. &ftp_gif('satellite photo', $satellite_photo_gif, $expire_time, 170000);
  307.  
  308. print FORM "#end\n";
  309.  
  310.  
  311.  
  312. &barf("problem closing formfile $formfile - $!") unless
  313.     close(FORM);
  314.  
  315. if ($debug) {
  316.   system '/bin/cat', $formfile;
  317. }
  318.  
  319. if ($debug) {
  320.   &sh("comp -form $formfile -noedit");
  321. }
  322. else {
  323.   &sh("comp -form $formfile -whatnowproc mhnsend");
  324. }
  325.  
  326. unlink $formfile;
  327. exit 0;
  328.  
  329.